﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Road3App.Model.CrossroadNs;
using Road3App.ViewModels;
using Road3App.Views.UserControls.EditControls;

namespace Road3App.Views.UserControls
{
    /// <summary>
    /// Interaction logic for EditCrossRoadUC.xaml
    /// </summary>
    public partial class EditCrossRoadUC : UserControl
    {
        private EditDimsPage editDims;
        private EditCellStatesPage editCellStates;
        private EditPhasesPage editPhases;

        private EditCrossRoadCellStatesViewModel _editCRCellStatesVM;
        private EditCrossRoadPhasesViewModel _editCRPhasesVM;

        private CrossRoad _cr;

        private EditCrossRoadUC()
        {
            InitializeComponent();
        }

        public EditCrossRoadUC(CrossRoad cr)
            : this()
        {
            _cr = cr;

            editDims = new EditDimsPage();
            frame.Navigate(editDims);    
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (frame.Content == editDims) // dims -> cell states
            {
                try
                {
                    int width = int.Parse(editDims.crWidth.Text);
                    int height = int.Parse(editDims.crHeight.Text);
                    _cr.CreateGrid(width, height);

                    editDims.message.Text = "";

                    _editCRCellStatesVM = new EditCrossRoadCellStatesViewModel(_cr);
                    editCellStates = new EditCellStatesPage() { DataContext = _editCRCellStatesVM };

                    frame.Navigate(editCellStates);
                }
                catch { editDims.message.Text = "failed to create grid"; }
            }
            else if (frame.Content == editCellStates) // cell states -> phases
            {
                _cr.PhaseCollection.Clear();
                _cr.Grid.Where(c => c.TrafficLight != null).ToList().ForEach(c => c.TrafficLight = null);
                _cr.CreateTrafficLights(); 

                _editCRPhasesVM = new EditCrossRoadPhasesViewModel(_cr);
                editPhases = new EditPhasesPage() { DataContext = _editCRPhasesVM };

                frame.Navigate(editPhases);
            }
            else if (frame.Content == editPhases) // phases -> Okay messsage
            {
                MessageBox.Show("now you can press OK\nand create CrossRoad");
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (frame.Content == editCellStates)
            {
                frame.Navigate(editDims);
            }
            else if (frame.Content == editPhases)
            {                
                frame.Navigate(editCellStates);                
            }
        }
    }
}
